//
//  LogoScreenViewController.m
//  FanTest
//
//  Created by Cheng on 12/6/28.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "LogoScreenViewController.h"
#import "RequestAPIFunction.h"
#import "AccountParser.h"
#import "LoadingUI.h"

#define USERINFO_API @"http://54.235.166.43/BeSpokeAPI/userInfo.php"
#define EDITTWITTEREMAIL_EMAILWIDTH 347
#define BETWEEN_USERNAME_EMAIL_SPACEWITDH 5

#define LOADINGVIEW_TAG 10000000
#define LOADINGIMAGE_TAG 20000000

@implementation LogoScreenViewController
@synthesize Vw_LoginMyAccount, Vw_CreateMyAccount, Vw_ForgotPassWord, BK_En_object, Array_AccountInfo;
@synthesize Vw_EditTwitterEmail;
@synthesize Bt_Screen;

bool Check_FirstUse = TRUE;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
        
    self.BK_En_object = [[BK_Enviroment alloc] init];
    self.Array_AccountInfo = [[NSMutableArray alloc] init];
    
    [Bt_Screen addTarget:self action:@selector(NonFirstPressed) forControlEvents:UIControlEventTouchUpInside];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

#pragma mark - Interface Orientation Management

//For iOS 5
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}

//For iOS 6
-(BOOL)shouldAutorotate {
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

#pragma mark - Segue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    if([[segue identifier] isEqualToString:@"Stream_Segue"]) {
        HomePageViewController *VC_Stream = segue.destinationViewController;
        VC_Stream.Array_AccountInfo = self.Array_AccountInfo;
        VC_Stream.BK_En_object = BK_En_object;
        VC_Stream.delegate = self;
    }
}

#pragma mark - LoginMyAccount

- (void)NonFirstPressed {
    //Check Logged in.
    if (BK_En_object.userName!=nil) {
        
        
        [NSThread detachNewThreadSelector:@selector(threadStartAnimating) toTarget:self withObject:nil];
        
        //Request Account API.
        NSData *responseData =
        [RequestAPIFunction XML_PostRequest:[NSString stringWithFormat:@"<Authentication UserName=\"%@\" Password=\"\" Name=\"\" Source=\"\" Plaintext=\"True\" />",BK_En_object.userName] urlString:USERINFO_API];
        
        
        
        //No Network Connection or time out.
        if (responseData == nil) {
            
            [NSThread detachNewThreadSelector:@selector(threadStopAnimating) toTarget:self withObject:nil];
            return;
        }
        
        self.Array_AccountInfo = [AccountParser loadAccountInfo:responseData];
        
        [self performSegueWithIdentifier:@"Stream_Segue" sender:self];
        [NSThread detachNewThreadSelector:@selector(threadStopAnimating) toTarget:self withObject:nil];
    }
    
    else {
        if (Vw_LoginMyAccount==nil) {
            
            Vw_LoginMyAccount = [[[NSBundle mainBundle] loadNibNamed:@"LoginMyAccount" owner:self options:nil] objectAtIndex: 0];
            Vw_LoginMyAccount.frame = CGRectMake(0, 0, Vw_LoginMyAccount.frame.size.width, Vw_LoginMyAccount.frame.size.height);
            
            Vw_LoginMyAccount.BK_En_object = self.BK_En_object;
            Vw_LoginMyAccount.Array_AccountInfo = self.Array_AccountInfo;
            
            Vw_LoginMyAccount.TF_email.delegate = Vw_LoginMyAccount;
            Vw_LoginMyAccount.TF_password.delegate = Vw_LoginMyAccount;
            Vw_LoginMyAccount.delegate = self;
        }
        [Vw_LoginMyAccount setAlpha:0];
        
        [UIView animateWithDuration:0.3
                         animations:^{[Vw_LoginMyAccount setAlpha:1.0];[self.view addSubview:Vw_LoginMyAccount];}
                         completion:^(BOOL finished){ }];
    }
}

#pragma mark - LoginMyAccountDelegate

- (void) SignInSuccess {
    
    self.Array_AccountInfo = Vw_LoginMyAccount.Array_AccountInfo;
    
    [UIView animateWithDuration:0.3
                     animations:^{[Vw_LoginMyAccount setAlpha:0.0];
                         [Vw_LoginMyAccount removeFromSuperview];}
                     completion:^(BOOL finished){
                         
                         [NSThread detachNewThreadSelector:@selector(threadStartAnimating) toTarget:self withObject:nil];
                         [self performSegueWithIdentifier:@"Stream_Segue" sender:self];
                         [NSThread detachNewThreadSelector:@selector(threadStopAnimating) toTarget:self withObject:nil];
                     }];
}


-(void) CreateAccountPressed_NonFirst {
    
    if (Vw_CreateMyAccount==nil) {
        
        Vw_CreateMyAccount = [[[NSBundle mainBundle] loadNibNamed:@"CreateMyAccount" owner:self options:nil] objectAtIndex:0];
        Vw_CreateMyAccount.frame = CGRectMake(0, 0, Vw_CreateMyAccount.frame.size.width, Vw_CreateMyAccount.frame.size.height);
        
        Vw_CreateMyAccount.Array_AccountInfo = self.Array_AccountInfo;
        Vw_CreateMyAccount.BK_En_object = self.BK_En_object;
        
        Vw_CreateMyAccount.Tf_FullName.delegate = Vw_CreateMyAccount;
        Vw_CreateMyAccount.Tf_Email.delegate = Vw_CreateMyAccount;
        Vw_CreateMyAccount.Tf_Password.delegate = Vw_CreateMyAccount;
        Vw_CreateMyAccount.delegate = self;
    }
    [Vw_CreateMyAccount setAlpha:0.0];
    
    [UIView animateWithDuration:0.3
                     animations:^{[Vw_CreateMyAccount setAlpha:1.0];[self.view addSubview:Vw_CreateMyAccount];}
                     completion:^(BOOL finished){
                         [Vw_LoginMyAccount setAlpha:0.0];
                     }];
}

- (void)ForgotPassWordPressed_NonFirst {
    
    if (Vw_ForgotPassWord==nil) {
        Vw_ForgotPassWord = [[[NSBundle mainBundle] loadNibNamed:@"ForgotPassWord" owner:self options:nil] objectAtIndex:0];
        Vw_ForgotPassWord.frame = CGRectMake(0, 0, Vw_ForgotPassWord.frame.size.width, Vw_ForgotPassWord.frame.size.height);
        
        Vw_ForgotPassWord.Tf_Email.delegate = Vw_ForgotPassWord;
        Vw_ForgotPassWord.delegate = self;
    }
    [Vw_ForgotPassWord setAlpha:0.0];
    
    [UIView animateWithDuration:0.3
                     animations:^{[Vw_ForgotPassWord setAlpha:1.0];[self.view addSubview:Vw_ForgotPassWord];}
                     completion:^(BOOL finished){
                         [Vw_LoginMyAccount setAlpha:0.0];
                     }];
}

/**
- (void)EditTwitterEmailPressed:(NSString *)UserName SetFullName:(NSString *)FullName {
    
    if (Vw_EditTwitterEmail==nil) {
        Vw_EditTwitterEmail = [[[NSBundle mainBundle] loadNibNamed:@"EditTwitterEmail" owner:self options:nil] objectAtIndex:0];
        Vw_EditTwitterEmail.frame = CGRectMake(0, 0, Vw_EditTwitterEmail.frame.size.width, Vw_EditTwitterEmail.frame.size.height);
        
        [Vw_EditTwitterEmail.Bt_CONTINUE.titleLabel setFont:[UIFont fontWithName:@"EamesCenturyModern-Regular" size:12]];
        Vw_EditTwitterEmail.NS_FullName = FullName;
        Vw_EditTwitterEmail.Array_AccountInfo = self.Array_AccountInfo;
        Vw_EditTwitterEmail.BK_En_object = self.BK_En_object;
        
        Vw_EditTwitterEmail.Lab_UserName.text = [NSString stringWithFormat:@"%@@",UserName];
        CGSize textSize = [[Vw_EditTwitterEmail.Lab_UserName text] sizeWithFont:[Vw_EditTwitterEmail.Lab_UserName font]];
        CGFloat strikeWidth = textSize.width;
        [Vw_EditTwitterEmail.Lab_UserName setFrame:CGRectMake(Vw_EditTwitterEmail.Lab_UserName.frame.origin.x,
                                                              Vw_EditTwitterEmail.Lab_UserName.frame.origin.y,
                                                              strikeWidth,
                                                              Vw_EditTwitterEmail.Lab_UserName.frame.size.height)];
        
        [Vw_EditTwitterEmail.Tf_Email setFrame:CGRectMake(Vw_EditTwitterEmail.Lab_UserName.frame.origin.x + Vw_EditTwitterEmail.Lab_UserName.frame.size.width +BETWEEN_USERNAME_EMAIL_SPACEWITDH,
                                                          Vw_EditTwitterEmail.Tf_Email.frame.origin.y,
                                                          EDITTWITTEREMAIL_EMAILWIDTH-strikeWidth-BETWEEN_USERNAME_EMAIL_SPACEWITDH,
                                                          Vw_EditTwitterEmail.Tf_Email.frame.size.height)];
        
        Vw_EditTwitterEmail.Tf_Email.delegate = Vw_EditTwitterEmail;
        Vw_EditTwitterEmail.delegate = self;
    }
    [Vw_EditTwitterEmail setAlpha:0.0];
    
    [UIView animateWithDuration:0.3
                     animations:^{[Vw_EditTwitterEmail setAlpha:1.0];[self.view addSubview:Vw_EditTwitterEmail];}
                     completion:^(BOOL finished){
                         [Vw_LoginMyAccount setAlpha:0.0];
                     }];
}**/

#pragma mark CreateMyAccountDelegate

-(void)SignUpSuccess {
    
    self.Array_AccountInfo = Vw_CreateMyAccount.Array_AccountInfo;
    
    [UIView animateWithDuration:0.3
                     animations:^{[Vw_CreateMyAccount setAlpha:0.0];
                         [Vw_LoginMyAccount removeFromSuperview];
                         [Vw_CreateMyAccount removeFromSuperview];}
                     completion:^(BOOL finished){
                         [self performSegueWithIdentifier:@"Stream_Segue" sender:self];
                     }];
    
}

- (void)CancelPressed_CreateMyAccount {
    
    [self.view bringSubviewToFront:Vw_LoginMyAccount];
    
    [UIView animateWithDuration:0.3
                     animations:^{[Vw_LoginMyAccount setAlpha:1.0];}
                     completion:^(BOOL finished){[Vw_CreateMyAccount setAlpha:0.0];[Vw_CreateMyAccount removeFromSuperview];}];
}

-(void) CancelPressed_ForgotPassWord {
    
    [self.view bringSubviewToFront:Vw_LoginMyAccount];
    
    [UIView animateWithDuration:0.3
                     animations:^{[Vw_LoginMyAccount setAlpha:1.0];}
                     completion:^(BOOL finished){[Vw_ForgotPassWord setAlpha:0.0];[Vw_ForgotPassWord removeFromSuperview];}];
}

#pragma mark - ForgotPasswordDelegate

-(void)EmailSuccess {
    [UIView animateWithDuration:0.3
                     animations:^{[Vw_ForgotPassWord setAlpha:0.0];}
                     completion:^(BOOL finished){[Vw_ForgotPassWord removeFromSuperview];[Vw_LoginMyAccount removeFromSuperview];}];
}

#pragma mark - EditTwitterEmailDelegate

-(void) CancelPressed_EditTwitterEmail {
    
    [self.view bringSubviewToFront:Vw_LoginMyAccount];
    [UIView animateWithDuration:0.3
                     animations:^{[Vw_LoginMyAccount setAlpha:1.0];}
                     completion:^(BOOL finished){[Vw_EditTwitterEmail setAlpha:0.0];[Vw_EditTwitterEmail removeFromSuperview];}];
}

-(void) SignInSuccess_UseTwitter {
    
    self.Array_AccountInfo = Vw_EditTwitterEmail.Array_AccountInfo;
    
    [UIView animateWithDuration:0.3
                     animations:^{[self.Vw_EditTwitterEmail setAlpha:0.0];
                         [self.Vw_LoginMyAccount removeFromSuperview];
                         [self.Vw_EditTwitterEmail removeFromSuperview];
                     }
                     completion:^(BOOL finished){
                         
                         [NSThread detachNewThreadSelector:@selector(threadStartAnimating) toTarget:self withObject:nil];
                         [self performSegueWithIdentifier:@"Stream_Segue" sender:self];
                         [NSThread detachNewThreadSelector:@selector(threadStopAnimating) toTarget:self withObject:nil];
                     }];
}

#pragma mark - FeedSetViewControllerDelegate

-(void)FeedSetViewControllerDelegateDidStream:(FeedSetViewController *)controller {
    
    [NSThread detachNewThreadSelector:@selector(threadStartAnimating) toTarget:self withObject:nil];
    [controller dismissViewControllerAnimated:NO completion:^{
        [self performSegueWithIdentifier:@"Stream_Segue" sender:controller];
        [NSThread detachNewThreadSelector:@selector(threadStopAnimating) toTarget:self withObject:nil];
    }];
}

#pragma mark - HomePageViewControllerDelegate

-(void)HomePageViewControllerDelegateDidLogoScreen:(HomePageViewController *)controller {

    
    [[self.view viewWithTag:103] removeFromSuperview];
    [[self.view viewWithTag:104] removeFromSuperview];
    [self dismissViewControllerAnimated:YES completion:nil];
    
    Vw_LoginMyAccount = nil;
    Vw_CreateMyAccount = nil;
    Vw_ForgotPassWord = nil;
    Vw_EditTwitterEmail = nil;
    
    //Clear Facebook Session.
    [FBSession.activeSession closeAndClearTokenInformation];
    [FBSession.activeSession close];
    [FBSession setActiveSession:nil];
    
    [Bt_Screen addTarget:self action:@selector(NonFirstPressed) forControlEvents:UIControlEventTouchUpInside];
    
    [controller dismissViewControllerAnimated:NO completion:nil];
}

#pragma mark - LoadingUI

-(void)threadStartAnimating {
    
    [self.view addSubview:[LoadingUI LoadingView:self.view.bounds.size.width SetHeight:self.view.bounds.size.height]];
    [self.view addSubview:[LoadingUI LoadingImage:self.view.bounds.size.width SetHeight:self.view.bounds.size.height]];
}

-(void)threadStopAnimating {
    
    [[NSOperationQueue mainQueue] addOperationWithBlock:
     ^(void) {
         [[self.view viewWithTag:LOADINGVIEW_TAG] removeFromSuperview];
         [[self.view viewWithTag:LOADINGIMAGE_TAG] removeFromSuperview];
     }];
}

@end
